﻿#ifndef __SS_READ_BYTE_H__
#define __SS_READ_BYTE_H__
#include <string>
#include "../ssBaseDefine.h"

namespace StarSeeker
{

class ssReadBytes
{
public:
	ssReadBytes();
	~ssReadBytes();
	ssReadBytes(const char* buffer, ulint size);
	//传入一个字节流
	void Set(const char* buffer, ulint size);
	//有没有初始化
	inline bool IsEmpty()
	{
		return m_buffer==0 || m_size==0;
	}
	// ReadChar
	inline bool ReadUChar(u8 &pdata)
	{
		return ReadBuffer(&pdata, cg_u8_size);
	}
	inline const u8* ReadUChar()
	{
		return (const u8*)ReadBuffer(cg_u8_size);
	}
	// ReadChar
	inline bool ReadChar(i8 &pdata)
	{
		return ReadBuffer(&pdata, cg_i8_size);
	}
	inline const i8* ReadChar()
	{
		return (const i8*)ReadBuffer(cg_i8_size);
	}
	// ReadFloat
	inline bool ReadFloat32(f32 &pdata)
	{
		return ReadBuffer(&pdata, cg_f32_size);
	}
	inline const f32* ReadFloat32()
	{
		return (const f32*)ReadBuffer(cg_f32_size);
	}
	// ReadI64
	inline bool ReadI64(i64 &pdata)
	{
		return ReadBuffer(&pdata, cg_i64_size);
	}
	inline const i64* ReadI64()
	{
		return (const i64*)ReadBuffer(cg_i64_size);
	}
	// ReadU64
	inline bool ReadU64(u64 &pdata)
	{
		return ReadBuffer(&pdata, cg_u64_size);
	}
	inline const u64* ReadU64()
	{
		return (const u64*)ReadBuffer(cg_u64_size);
	}
	// ReadDouble
	inline bool ReadFloat64(f64 &pdata)
	{
		return ReadBuffer(&pdata, cg_f64_size);
	}
	// ReadU32
	inline bool ReadU32(u32& pdata)
	{
		return ReadBuffer(&pdata, cg_u32_size);
	}
	inline const u32* ReadU32()
	{
		return (const u32*)ReadBuffer(cg_u32_size);
	}
	// ReadUL32
	inline bool ReadULong(ulint& pdata)
	{
		return ReadBuffer(&pdata, cg_ulint_size);
	}
	inline const ulint* ReadULong()
	{
		return (const ulint*)ReadBuffer(cg_ulint_size);
	}
	// ReadI32
	inline bool ReadI32(i32& pdata)
	{
		return ReadBuffer(&pdata, cg_i32_size);
	}
	inline const i32* ReadI32()
	{
		return (const i32*)ReadBuffer(cg_i32_size);
	}
	// Readi16
	inline bool ReadI16(i16& pdata)
	{
		return ReadBuffer(&pdata, cg_i16_size);
	}
	inline const i16* ReadI16()
	{
		return (const i16*)ReadBuffer(cg_i16_size);
	}
	// ReadU16
	inline bool ReadU16(u16& pdata)
	{
		return ReadBuffer(&pdata, cg_u16_size);
	}
	inline const u16* ReadU16()
	{
		return (const u16*)ReadBuffer(cg_u16_size);
	}
	// ReadData
	inline bool ReadData(void* pdata, u32 size)
	{
		return ReadBuffer(pdata, size);
	}
	inline const char* ReadData(u32 size)
	{
		return ReadBuffer(size);
	}
	// ReadString
	u32 ReadString(char* pbuf, u32 bufSize);
	bool ReadString(std::string& str);
	// 读结构
	u32 ReadStruct(void* pbuf, u32 bufSize);
	// 跳过指定个字节
	bool Skip(ulint size) const;
	// 获取当前指针
	inline const char * CurrentBuf() const
	{
		return m_buffer+m_pos;
	}
	// 获取起始指针
	inline const char * Buf() const
	{
		return m_buffer + m_pos;
	}
	// 剩余大小
	inline ulint RemainSize() const
	{
		return m_size-m_pos;
	}
	// 已读的大小
	inline ulint ReadSize() const
	{
		return m_pos;
	}
	// 总大小
	inline ulint MaxSize() const
	{
		return m_size;
	}
	//读取一行
	ulint GetLine(char* pbuf, u32 bufSize);

private:
	bool ReadBuffer(void* p, ulint size);
	const char* ReadBuffer(ulint size);

	const char*		m_buffer;	// 缓存地址
	mutable ulint	m_pos;		// 已读的大小
	ulint			m_size;		// 缓存总长度
};

}
#endif
